﻿using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;

namespace M2Server
{
    public class ObjectSystem
    {
        /// <summary>
        /// 精灵对象列表
        /// </summary>
        private readonly ConcurrentDictionary<long, TBaseObject> s_Actors = new ConcurrentDictionary<long, TBaseObject>();
        /// <summary>
        /// 其他对象
        /// </summary>
        private readonly ConcurrentDictionary<long, object> Ohter = new ConcurrentDictionary<long, object>();
        private readonly Timer _actorTime = null;

        public ObjectSystem()
        {
            _actorTime = new Timer(DoWork, null, 30000, 60000);//定时移除已经挂掉的对象
            Console.WriteLine("Start Clear Actor ThreadTime...");
        }

        public void Add(long actorId, TBaseObject actor)
        {
            s_Actors.TryAdd(actorId, actor);
        }

        public void AddOhter(int objectId, object obj)
        {
            Ohter.TryAdd(objectId, obj);
        }

        public object GetOhter(uint objectId)
        {
            object obj = null;
            if (Ohter.TryGetValue(objectId, out obj))
            {
                return obj;
            }
            return null;
        }

        public TBaseObject Get(int actorId)
        {
            TBaseObject actor = null;
            if (s_Actors.TryGetValue(actorId, out actor))
            {
                return actor;
            }
            return actor;
        }

        public void Revome(long actorId)
        {
            TBaseObject actor = null;
            s_Actors.TryRemove(actorId, out actor);
            if (actor != null)
            {
                if (actor.m_LastHiter != null)
                {
                    M2Share.MainOutMessage(string.Format("actors [{0}] Id [{1}] kill [{2}]", actor.m_sCharName, actorId,actor.m_LastHiter.m_sCharName));
                    Debug.WriteLine(string.Format("actors [{0}] Id [{1}] kill [{2}]", actor.m_sCharName, actorId, actor.m_LastHiter.m_sCharName));
                }
                else
                {
                    Debug.WriteLine(string.Format("Actors [{0}] Id [{1}]", actor.m_sCharName, actorId));
                    M2Share.MainOutMessage(string.Format("Actors [{0}] Id [{1}]", actor.m_sCharName, actorId));
                }
            }
        }

        public void RevomeOhter(long actorId)
        {
            object actor = null;
            Ohter.TryRemove(actorId, out actor);
            if (actor != null)
            {
                Debug.WriteLine(string.Format("Delete Ohter [{0}]", actorId));
                M2Share.MainOutMessage(string.Format("Delete Ohter [{0}]", actorId));
            }
        }

        internal void DoWork(object obj)
        {
            var actorIds = s_Actors.Keys;
            TBaseObject actor = null;
            foreach (var actorId in actorIds)
            {
                if (s_Actors.TryGetValue(actorId, out actor))
                {
                    if (actor.m_boDeath || actor.m_boGhost)
                    {
                        Revome(actorId);
                    }
                }
            }
        }
    }
}